﻿"use strict";
define(['admin/app', "service/admin/goodDeclarationService"], function (app) {
    app.controller('goodDeclarationController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, $uibModal, goodDeclarationService) {
        $scope.psdisabled = false;
        $scope.query = { page: 1, size: 20, mobile: '', auth_status: '' };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                goodDeclarationService.gets($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    goodDeclarationService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.model.audit_status = response.data.audit_status.toString();
                        $scope.model.unit = response.data.unit.toString();
                        $scope.model.min_unit = response.data.min_unit.toString();
                    });
                    $scope.breadcrumb = '编辑';
                    if ($stateParams.action == "audit") {
                        $scope.breadcrumb = '审核';
                        $scope.action = "audit";
                    }
                }
                else {
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    good_sku: { required: '请填写商品货号SKU' },
                    mobile: { required: '请填写手机号码' }
                });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            code: function () {
                goodDeclarationService.getcode().success(function (response) {
                    $scope.code = response.data;
                });
            },
            get_company_type: function (id) {
                var company_type = new Array("全部", "贸易", "销售");
                return company_type[id];
            },
            get_audit_status: function (id) {
                var audit_status = new Array("待缴费", "审核中", "报关中", "完成", "失败", "不通过");
                return audit_status[id];
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                if ($stateParams.id > 0) {
                    if ($stateParams.action != "audit")
                        goodDeclarationService.alter($scope.model).success(function (response) {
                            utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                            if (response.result_code == 1000)
                                methods.goBack();
                            else
                                $scope.psdisabled = false;
                        }).error(function () { $scope.psdisabled = false; });
                    else
                        goodDeclarationService.audit($scope.model).success(function (response) {
                            utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                            if (response.result_code == 1000)
                                methods.goBack();
                            else
                                $scope.psdisabled = false;
                        }).error(function () { $scope.psdisabled = false; });
                }
                else {
                    goodDeclarationService.post($scope.model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除当前商户", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    goodDeclarationService.delete(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000) {
                            utils.remove($scope.model, model); methods.search();
                        }
                    
                    })
                })
            }
        }
        angular.extend($scope, methods);
        if ($stateParams.action != undefined) {
            methods.edit();
        }
        else
            methods.search();
    });
});
